package com.ccc.boot.security.controller;

import com.ccc.boot.response.ResponseCode;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.security.cache.CaptchaCache;
import com.ccc.boot.security.constant.AuthConstant;
import com.ccc.boot.security.pojo.LoginParam;
import com.ccc.boot.security.service.AuthService;
import com.ccc.boot.util.Base64Utils;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统登录——REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022/3/24
 */
@RestController
@RequestMapping(AuthConstant.LOGIN_URI_PREFIX)
public class LoginController {
    protected Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private AuthService authService;

    @Autowired
    private CaptchaCache captchaCache;

    /**
     * web管理系统——登录
     *
     * @param loginParam
     * @param request
     * @return {@link ResponseResult}
     * @date 2022/3/29 17:10
     * @author superC
     */
    @PostMapping("/wmsLogin")
    public ResponseResult wmsLogin(@RequestBody LoginParam loginParam, HttpServletRequest request) {
        return authService.wmsLogin(loginParam, request);
    }

    /**
     * app——手机号码登录
     *
     * @param loginParam
     * @param request
     * @return {@link ResponseResult}
     * @date 2022/3/29 17:12
     * @author superC
     */
    @PostMapping("/appLogin")
    public ResponseResult appLogin(@RequestBody LoginParam loginParam, HttpServletRequest request) {
        return authService.appLogin(loginParam, request);
    }


    /**
     * 获取验证码
     *
     * @param random
     */
    @GetMapping("/captcha/{random}")
    public ResponseResult captcha(@PathVariable("random") String random) {
        Map<String, Object> map = new HashMap<>();
        // 生成图片验证码
        ByteArrayOutputStream outputStream = null;
        try {
            // 生成文字验证码
            String text = defaultKaptcha.createText();

            // 存储captcha至redis
            captchaCache.storage2Redis(random, text);

            BufferedImage image = defaultKaptcha.createImage(text);

            outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", outputStream);

            // 获取图片的Base64编码字符串
            map.put("img", Base64Utils.getImageBase64(outputStream.toByteArray()));
        } catch (Exception e) {
            logger.error("【捕获异常-获取验证码】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.LOGIN_GET_CAPTCHR_FAILURE);
        }
        return ResponseResult.success("获取验证码成功", map);
    }


}